package com.zx.mes.senior.reflect;

import lombok.extern.java.Log;
import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Stream;

/**
 * reflect test
 *
 * @author 华云龙
 * @date 2018-8-30
 */
@Log
public class ReflectTest {

    @Test
    public void test() throws IllegalAccessException, InstantiationException {
        Class<?> clazz = Person.class;
        log.info(clazz.toString());
        ClassLoader classLoader = clazz.getClassLoader();
        log.info(classLoader.toString());

        Person person = (Person)clazz.newInstance();
        person.setName("测试");
        person.setAge(10);
        log.info(person.toString());

    }

    @Test
    public void test2() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> clazz = Person.class;
        Constructor<?>[] constructors = clazz.getConstructors();
        /// 遍历所有的构造函数
        Arrays.stream(constructors).forEach(constructor -> log.info(constructor.toString()));

        /// 获取指定的构造函数
        Constructor<?> constructor = clazz.getConstructor(String.class, int.class);
        log.info(constructor.toString());

        Person person = (Person) constructor.newInstance("神一样的", 20);
        log.info(person.toString());

    }





















    /**
     *
     */
    @Test
    public void test3() throws IllegalAccessException, InstantiationException {
        /// 1.
        Class<Person> clazz = Person.class;
        Person person = clazz.newInstance();
        person.setAge(24);
        person.setName("聂婷");
        System.out.println(person.toString());
    }

    @Test
    public void test4(){
        Person person = new Person();
        Class<? extends Person> aClass = person.getClass();
        System.out.println(aClass);

    }

    @Test
    public void test5() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> clazz = Class.forName("com.zx.mes.senior.reflect.Person");
        Person person = (Person)clazz.newInstance();
        person.setName("聂婷");
        System.out.println(person.toString());
    }

    @Test
    public void test6() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        Class<Person> clazz = Person.class;
        Constructor<Person> constructor = clazz.getConstructor(String.class, int.class);
        System.out.println("------------------ 反射clazz.getMethods()所有公有方法(除构造方法) -----------------------");
        Method[] methods = clazz.getMethods();
        Stream.of(methods).forEach(System.out::println);
        System.out.println("------------------ clazz.getDeclaredMethods()自己声明的方法(除构造方法) -----------------------");
        Method[] methods2 = clazz.getDeclaredMethods();
        Stream.of(methods2).forEach(System.out::println);

        System.out.println("---------------------------- 隔离 ------------------------------");

        System.out.println("------------------ filed -----------------------");
        Field[] fields = clazz.getFields();
        Stream.of(fields).forEach(System.out::println);

        System.out.println("------------------ 自定义filed -----------------------");
        Field[] fields2 = clazz.getDeclaredFields();
        Stream.of(fields2).forEach(System.out::println);



        System.out.println("------------------------- 隔离  ----------------------------");
        System.out.println(constructor);
        Person person = constructor.newInstance("聂婷2", 242);
        Field age = clazz.getDeclaredField("age");
        age.setAccessible(true);
        age.set(person,25);
        System.out.println(person.toString());
    }


}
